package com.sitech.openapi.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sitech.openapi.componet.BusiConst;
import com.sitech.openapi.componet.MapBean;
import com.sitech.openapi.componet.WSClientService;
import com.sitech.openapi.domain.Invoice;
import com.sitech.openapi.domain.InvoiceInformation;
import com.sitech.openapi.domain.IssueInvoice;
import com.sitech.openapi.domain.OrderOneItem;
import com.sitech.openapi.domain.invoice.ElectInvoIssuItemsDetail;
import com.sitech.openapi.domain.invoice.ElectInvoiceDetail;
import com.sitech.openapi.domain.invoice.ElectInvoiceIssuDetail;
import com.sitech.openapi.domain.invoice.ElectInvoiceIssuDetailExample;
import com.sitech.openapi.mapper.*;
import com.sitech.openapi.utils.DateUtil;
import com.sitech.openapi.utils.NumberUtil;
import com.sitech.openapi.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;
import java.util.regex.Pattern;

import org.springframework.web.client.RestTemplate;


/**
 * add by wangdj 2018/12/11
 * 关于服务一致性二期优化工作支撑--电子发票
 */
@Service
@Slf4j
public class InvoiceInfoServiceImpl implements InvoiceInfoService {
    @Autowired
    private InvoiceInfoMapper invoiceInfoMapper;
    @Autowired
    private ProdInstAccNbrMapper prodInstAccNbrMapper;
    @Autowired
    WSClientService wsClientService;
    @Autowired
    private ElectInvoiceIssuDetailMapper electInvoiceIssuDetailMapper;
    @Autowired
    private ElectInvoIssuItemsDetailMapper electInvoIssuItemsDetailMapper;
    @Autowired
    private ElectInvoiceDetailMapper electInvoiceDetailMapper;
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private QryCustInfoMapper qryCustInfoMapper;
    @Autowired
    private QryCustInfoService qryCustInfoService;
    /**
     * 查询营业订单类型的增值税普通电子发票
     * @param serialNumber
     * @return
     */
    @Override
    public List<OrderOneItem> getOrderOneItems(String serialNumber,String lanId){
        List<OrderOneItem> orderOneItems = new ArrayList<OrderOneItem>();
        //查询用户实例ID——省内
        String prodInstId = StringUtil.obj2Str(invoiceInfoMapper.getProdInstIdByAccNbr(serialNumber));
        if(StringUtil.isEmptyOrNull(prodInstId)){
            throw new RuntimeException("营业订单费用对应的产品实例ID不存在");
        }

        //首先通过号码查询正表中的电子发票信息
        Date nowDate = DateUtil.change(new Date(),2,-5);
        String startDate = DateUtil.toStringYm(nowDate);
        String endDate = DateUtil.toStringYm(new Date());
        Map<String,String> nowMap = new HashMap<String,String>();
        nowMap.put("serialNumber",serialNumber);
        nowMap.put("startDate",startDate);
        nowMap.put("endDate",endDate);
        List<Map<String,String>> custOrderIdListNow = invoiceInfoMapper.selectCustOrderByAccNbrNow(nowMap);
        int custOrderIdListNowSize = custOrderIdListNow.size();
        if(custOrderIdListNowSize>0){
            //通过循环查询出来的客户订单号获取发票信息
            for(int b=0;b<custOrderIdListNowSize;b++){
                String custOrderIdNow = StringUtil.obj2Str(custOrderIdListNow.get(b).get("CUSTORDERID"));
                nowMap.put("suffix","");
                nowMap.put("custOrderId",custOrderIdNow);
                //通过客户订单号查询该订单下所有的费用项
                List<Map<String,String>> amountListNow = invoiceInfoMapper.selectAmountByCustOrderId(nowMap);
                int amountListNowSize = amountListNow.size();
                if(amountListNowSize>0){
                    OrderOneItem orderOneItem = new OrderOneItem();
                    double amountsNow = 0;
                    for(int c=0;c<amountListNowSize;c++){
                        double amountNow = NumberUtil.getDoubleFromObj(amountListNow.get(c).get("AMOUNT"));
                        amountsNow = amountsNow + amountNow;
                    }
                    //接口协议变更——所有的金额单位都是“分”
                    amountsNow = amountsNow*100;
                    String billId = StringUtil.obj2Str(amountListNow.get(0).get("BILLID"));
                    //判断该客户订单是否开具过发票
                    if(StringUtil.isEmptyOrNull(amountListNow.get(0).get("INVOICEID"))){
                        //该客户订单未开具过发票，给 OrderOneItem 对象赋值
                        orderOneItem.setID(billId);
                        orderOneItem.setProductId(prodInstId);
                        orderOneItem.setOrderNumber(custOrderIdNow);
                        orderOneItem.setAmount(Double.toString(amountsNow));
                        String formatOrderDate = qryCustInfoService.formatDateTime(StringUtil.obj2Str(custOrderIdListNow.get(b).get("ACCEPTTIME")));
                        orderOneItem.setOrderDate(formatOrderDate);
                        orderOneItem.setStatus("2");
                        orderOneItem.setInvoiceId("");
                        orderOneItems.add(orderOneItem);
                    }else{
                        String invoiceId = StringUtil.obj2Str(amountListNow.get(0).get("INVOICEID"));
                        /**
                         * 已开具：包括已开具和开具中
                         */
                        //根据发票实例ID查询发票开具状态
                        ElectInvoiceIssuDetailExample electInvoiceIssuDetailExample = new ElectInvoiceIssuDetailExample();
                        Long eleInvoiisueDel = NumberUtil.parseLong(invoiceId);
                        electInvoiceIssuDetailExample.createCriteria().andElectInvoiissuDtlIdEqualTo(eleInvoiisueDel);
                        List<ElectInvoiceIssuDetail> electInvoiceIssuDetails = electInvoiceIssuDetailMapper.selectByExample(electInvoiceIssuDetailExample);
                        int electInvoiceIssuDetailsSize = electInvoiceIssuDetails.size();
                        String statusCd = "";
                        if(electInvoiceIssuDetailsSize>0){
                            String statusCd0 = StringUtil.obj2Str(electInvoiceIssuDetails.get(0).getStatusCd());
                            if(StringUtil.isEqual("0",statusCd0)){
                                statusCd = "3";
                            }else{
                                statusCd = "1";
                            }
                        }
                        orderOneItem.setID(billId);
                        orderOneItem.setProductId(prodInstId);
                        orderOneItem.setOrderNumber(custOrderIdNow);
                        orderOneItem.setAmount(Double.toString(amountsNow));
                        String formatOrderDate = qryCustInfoService.formatDateTime(StringUtil.obj2Str(custOrderIdListNow.get(b).get("ACCEPTTIME")));
                        orderOneItem.setOrderDate(formatOrderDate);
                        orderOneItem.setStatus(statusCd);
                        orderOneItem.setInvoiceId(invoiceId);
                        orderOneItems.add(orderOneItem);
                    }
                }
            }
        }
        //查询历史表——最近6个月的信息
        for(int k=0;k<6;k++){
            Date date = DateUtil.change(new Date(),2,-k);
            String suffix = DateUtil.toStringYm(date);
            Map<String,String> inMap = new HashMap<String,String>();
            inMap.put("suffix",suffix);
            inMap.put("serialNumber",serialNumber);
            /**
             * 未开具：查询客户订单下的所有未开具发票的费用项，加起来。
             */
            //查询本月用户下所有的客户订单号
            List<Map<String,String>> custOrderIdList = invoiceInfoMapper.selectCustOrderIdByAccNbr(inMap);
            int custOrderIdListSize  = custOrderIdList.size();
            if(custOrderIdListSize>0){
                for(int i=0;i<custOrderIdListSize;i++){
                    String custOrderId = StringUtil.obj2Str(custOrderIdList.get(i).get("CUSTORDERID"));
                    //通过客户订单查询所有的费用
                    inMap.put("custOrderId",custOrderId);
                    List<Map<String,String>> amountList = invoiceInfoMapper.selectAmountByCustOrderId(inMap);
                    int amountListSize = amountList.size();
                    if(amountListSize>0){
                        OrderOneItem orderOneItem = new OrderOneItem();
                        double amounts = 0;
                        for(int a=0;a<amountListSize;a++){
                            double amount = NumberUtil.getDoubleFromObj(amountList.get(a).get("AMOUNT"));
                            amounts = amounts+amount;
                        }
                        //接口协议变更——所有的金额单位都是“分”
                        amounts = amounts*100;
                        //判断该客户订单是否开具过发票
                        String billId = StringUtil.obj2Str(amountList.get(0).get("BILLID"));
                        if(StringUtil.isEmptyOrNull(amountList.get(0).get("INVOICEID"))){
                            //改客户订单未开具过发票给 OrderOneItem 对象赋值
                            orderOneItem.setID(billId);
                            orderOneItem.setProductId(prodInstId);
                            orderOneItem.setOrderNumber(custOrderId);
                            orderOneItem.setAmount(Double.toString(amounts));
                            String formatOrderDate = qryCustInfoService.formatDateTime(StringUtil.obj2Str(custOrderIdList.get(i).get("ACCEPTTIME")));
                            orderOneItem.setOrderDate(formatOrderDate);
                            orderOneItem.setStatus("2");
                            orderOneItem.setInvoiceId("");
                            orderOneItems.add(orderOneItem);
                        }else{
                            String invoiceId = StringUtil.obj2Str(amountList.get(0).get("INVOICEID"));
                            /**
                             * 已开具：包括已开具和开具中
                             */
                            //根据发票实例ID查询发票开具状态
                            ElectInvoiceIssuDetailExample electInvoiceIssuDetailExample = new ElectInvoiceIssuDetailExample();
                            Long eleInvoiisueDel = NumberUtil.parseLong(invoiceId);
                            electInvoiceIssuDetailExample.createCriteria().andElectInvoiissuDtlIdEqualTo(eleInvoiisueDel);
                            List<ElectInvoiceIssuDetail> electInvoiceIssuDetails = electInvoiceIssuDetailMapper.selectByExample(electInvoiceIssuDetailExample);
                            int electInvoiceIssuDetailsSize = electInvoiceIssuDetails.size();
                            String statusCd = "";
                            if(electInvoiceIssuDetailsSize>0){
                                String statusCd0 = StringUtil.obj2Str(electInvoiceIssuDetails.get(0).getStatusCd());
                                if(StringUtil.isEqual("0",statusCd0)){
                                    statusCd = "3";
                                }else{
                                    statusCd = "1";
                                }
                            }
                            orderOneItem.setID(billId);
                            orderOneItem.setProductId(prodInstId);
                            orderOneItem.setOrderNumber(custOrderId);
                            orderOneItem.setAmount(Double.toString(amounts));
                            String formatOrderDate = qryCustInfoService.formatDateTime(StringUtil.obj2Str(custOrderIdList.get(i).get("ACCEPTTIME")));
                            orderOneItem.setOrderDate(formatOrderDate);
                            orderOneItem.setStatus(statusCd);
                            orderOneItem.setInvoiceId(invoiceId);
                            orderOneItems.add(orderOneItem);
                        }
                    }
                }
            }
        }
        return orderOneItems;
    }

    /**
     * 新增开票信息
     * @param invoiceInformation
     * @return
     */
    @Override
    public InvoiceInformation addInvoiceInfo(InvoiceInformation invoiceInformation) {
        if(StringUtils.isEmpty(invoiceInformation)||StringUtils.isEmpty(invoiceInformation.getCustId())){
            throw new RuntimeException("开票信息为空或客户编码为空");
        }
        String custId = String.valueOf(invoiceInformation.getCustId());
        List<Map<String,String>> checkList = invoiceInfoMapper.selectInvoiceInfoByCustId(custId);
        InvoiceInformation invoiceInformation1 = getInvoiceInformation(checkList);

        if(StringUtil.isNotEmptyOrNull(invoiceInformation1.getId())&&
                StringUtil.isNotEmptyOrNull(invoiceInformation1.getMobile())&&
                StringUtil.isNotEmptyOrNull(invoiceInformation1.geteMail())&&
                StringUtil.isNotEmptyOrNull(invoiceInformation1.getAutomaticPush())&&
                StringUtil.isNotEmptyOrNull(invoiceInformation1.getLanId())&&
                StringUtil.isNotEmptyOrNull(invoiceInformation1.getInvoiceHeader())){
            throw new RuntimeException("该开票信息已经存在，不允许重复增加");
        }

        //验证输入的开票信息是否存在必填项为空的情况
        if(StringUtil.isEmptyOrNull(invoiceInformation.getId())||
            StringUtil.isEmptyOrNull(invoiceInformation.getMobile())||
            StringUtil.isEmptyOrNull(invoiceInformation.geteMail())||
            StringUtil.isEmptyOrNull(invoiceInformation.getAutomaticPush())||
            StringUtil.isEmptyOrNull(invoiceInformation.getLanId())||
            StringUtil.isEmptyOrNull(invoiceInformation.getInvoiceHeader())){
            throw new RuntimeException("发票信息必填项存在为空的情况，请核查");
        }

        Map<String,String> invoiceInfoMap = new HashMap<String,String>();
        invoiceInfoMap.put("custId",custId);

        String id = StringUtil.obj2Str(invoiceInformation.getId());
        invoiceInfoMap.put("attrId","999024");
        invoiceInfoMap.put("attrValue",id);
        invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);

        String mobile = StringUtil.obj2Str(invoiceInformation.getMobile());
        invoiceInfoMap.put("attrId","999032");
        invoiceInfoMap.put("attrValue",mobile);
        invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);

        String email = StringUtil.obj2Str(invoiceInformation.geteMail());
        invoiceInfoMap.put("attrId","999033");
        invoiceInfoMap.put("attrValue",email);
        invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);

        String automaticPush = StringUtil.obj2Str(invoiceInformation.getAutomaticPush());
        invoiceInfoMap.put("attrId","999025");
        invoiceInfoMap.put("attrValue",automaticPush);
        invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);

        String lanId = StringUtil.obj2Str(invoiceInformation.getLanId());
        invoiceInfoMap.put("attrId","999034");
        invoiceInfoMap.put("attrValue",lanId);
        invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);

        String invoiceHeader = StringUtil.obj2Str(invoiceInformation.getInvoiceHeader());
        invoiceInfoMap.put("attrId","999026");
        invoiceInfoMap.put("attrValue",invoiceHeader);
        invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);

        if(!StringUtils.isEmpty(invoiceInformation.getBuyerTaxPayerId())){
            String buyerTaxPayerId = StringUtil.obj2Str(invoiceInformation.getBuyerTaxPayerId());
            invoiceInfoMap.put("attrId","999027");
            invoiceInfoMap.put("attrValue",buyerTaxPayerId);
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }else{
            invoiceInfoMap.put("attrId","999027");
            invoiceInfoMap.put("attrValue","");
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }

        if(!StringUtils.isEmpty(invoiceInformation.getBuyerAddress())){
            String buyerAddress = StringUtil.obj2Str(invoiceInformation.getBuyerAddress());
            invoiceInfoMap.put("attrId","999028");
            invoiceInfoMap.put("attrValue",buyerAddress);
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }else{
            invoiceInfoMap.put("attrId","999028");
            invoiceInfoMap.put("attrValue","");
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }

        if(!StringUtils.isEmpty(invoiceInformation.getBuyerPhone())){
            String buyerPhone = StringUtil.obj2Str(invoiceInformation.getBuyerPhone());
            invoiceInfoMap.put("attrId","999029");
            invoiceInfoMap.put("attrValue",buyerPhone);
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }else{
            invoiceInfoMap.put("attrId","999029");
            invoiceInfoMap.put("attrValue","");
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }

        if(!StringUtils.isEmpty(invoiceInformation.getBackAccount())){
            String bankAccount = StringUtil.obj2Str(invoiceInformation.getBackAccount());
            invoiceInfoMap.put("attrId","999031");
            invoiceInfoMap.put("attrValue",bankAccount);
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }else{
            invoiceInfoMap.put("attrId","999031");
            invoiceInfoMap.put("attrValue","");
            invoiceInfoMapper.insertInvoiceInfo(invoiceInfoMap);
        }
        List<Map<String,String>> invoiceInfoList = invoiceInfoMapper.selectInvoiceInfoByCustId(custId);
        InvoiceInformation invoiceInformation0 = getInvoiceInformation(invoiceInfoList);
        return invoiceInformation0;
    }

    /**
     * 查询开票信息——开票信息API
     * @param serialNumber
     * @return
     */
    @Override
    public InvoiceInformation getInvoiceInfo(String serialNumber,String type,String lanId) {
        List<Map<String,String>> invoiceInfoList = invoiceInfoMapper.selectInvoiceInfo(serialNumber);
        InvoiceInformation invoiceInformation = getInvoiceInformation(invoiceInfoList);
        return invoiceInformation;
    }

    /**
     * 更新开票信息——《开票信息接口》
     * @param id
     * @param attrInfoList
     * @return
     */
    public InvoiceInformation updateInvoiceInfo(String id,List<Map<String,String>> attrInfoList){
        String custId = StringUtil.obj2Str(invoiceInfoMapper.getCustIdById(id));
        if(StringUtil.isEmptyOrNull(custId)){
            throw new RuntimeException("该开票信息不存在，请核查");
        }
        int attrInfoListSize = attrInfoList.size();
        if(attrInfoListSize>0){
            for(int i=0;i<attrInfoListSize;i++){
                String op = StringUtil.obj2Str(attrInfoList.get(i).get("op"));
                String[] symbolName = StringUtil.obj2Str(attrInfoList.get(i).get("path")).split("/");
                if(op.equals("replace")&&symbolName[1].equals("invoiceInformation")){
                    Map<String,String> inMap = new HashMap<String,String>();
                    inMap.put("custId",custId);
                    String attrName = symbolName[2].toUpperCase();
                    String attrValue = String.valueOf(attrInfoList.get(i).get("value"));
//                    if(attrName.equals("MOBILE")){
//                        inMap.put("attrId","999032");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
                    if(attrName.equals("EMAIL")){
                        inMap.put("attrId","999033");
                        inMap.put("attrValue",attrValue);
                        invoiceInfoMapper.updateInvoiceInfo(inMap);
                    }
//                    if(attrName.equals("AUTOMATICPUSH")){
//                        inMap.put("attrId","999025");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
//                    if(attrName.equals("LANID")){
//                        inMap.put("attrId",""999034);
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
//                    if(attrName.equals("INVOICEHEADER")){
//                        inMap.put("attrId","999026");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
//                    if(attrName.equals("BUYERTAXPAYERID")){
//                        inMap.put("attrId","999027");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
//                    if(attrName.equals("BUYERADDRESS")){
//                        inMap.put("attrId","999028");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
//                    if(attrName.equals("BUYERPHONE")){
//                        inMap.put("attrId","999029");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
//                    if(attrName.equals("BANKACCOUNT")){
//                        inMap.put("attrId","999031");
//                        inMap.put("attrValue",attrValue);
//                        invoiceInfoMapper.updateInvoiceInfo(inMap);
//                    }
                }

            }
        }
        List<Map<String,String>> invoiceInfoList = invoiceInfoMapper.selectInvoiceInfoByCustId(custId);
        InvoiceInformation invoiceInformation = getInvoiceInformation(invoiceInfoList);
        return invoiceInformation;
    }

    /**
     * 因为开票信息为纵向列表，所以封装成一个方法。
     * @param invoiceInfoList
     * @return
     */
    public InvoiceInformation getInvoiceInformation(List<Map<String,String>> invoiceInfoList){
        int invoiceInfoListSize = invoiceInfoList.size();
        InvoiceInformation invoiceInformation = new InvoiceInformation();
        if(invoiceInfoListSize>0){
            invoiceInformation.setCustId(StringUtil.obj2Str(invoiceInfoList.get(0).get("CUSTID")));
            for(int i=0;i<invoiceInfoListSize;i++){
                String attrId = StringUtil.obj2Str(invoiceInfoList.get(i).get("ATTRID"));
                String attrValue = StringUtil.obj2Str(invoiceInfoList.get(i).get("ATTRVALUE"));
                if(!StringUtils.isEmpty(attrId)){
                    if(attrId.equals("999024")){
                        invoiceInformation.setId(attrValue);
                    }
                    if(attrId.equals("999032")){
                        invoiceInformation.setMobile(attrValue);
                    }
                    if(attrId.equals("999033")){
                        invoiceInformation.seteMail(attrValue);
                    }
                    if(attrId.equals("999025")){
                        invoiceInformation.setAutomaticPush(attrValue);
                    }
                    if(attrId.equals("999034")){
                        invoiceInformation.setLanId(attrValue);
                    }
                    if(attrId.equals("999026")){
                        invoiceInformation.setInvoiceHeader(attrValue);
                    }
                    if(attrId.equals("999027")){
                        invoiceInformation.setBuyerTaxPayerId(attrValue);
                    }
                    if(attrId.equals("999028")){
                        invoiceInformation.setBuyerAddress(attrValue);
                    }
                    if(attrId.equals("999029")){
                        invoiceInformation.setBuyerPhone(attrValue);
                    }
                    if(attrId.equals("999031")){
                        invoiceInformation.setBackAccount(attrValue);
                    }
                }
            }
        }
        return invoiceInformation;
    }
    /**
     * 调中兴接口开具发票——开具发票API
     * @param issueInvoice
     * @return
     */
    public void doIssueInvoice(IssueInvoice issueInvoice,HttpHeaders headers){
        if(StringUtil.isEmptyOrNull(issueInvoice.getType())){
            throw new RuntimeException("电子发票类型不能为空");
        }
        String type = StringUtil.obj2Str(issueInvoice.getType());
        if(type.equals("1200")){

            OrderOneItem[] orderOneItem0 = issueInvoice.getObjects();
            if(StringUtil.isNotEmptyOrNull(orderOneItem0)){
                int orderOneItemLength = orderOneItem0.length;
                for (int k=0;k<orderOneItemLength;k++){
                    //默认客户订单在正表中
                    boolean hisFlag = false;
                    Map<String,String> inMap = new HashMap<String,String>();
                    OrderOneItem orderOneItem = orderOneItem0[k];
                    String custOrderId = "";
                    Map<String,String> customerOrderInfoMap = new HashMap<String,String>();
                    String suffix = "";
                    if(StringUtils.isEmpty(orderOneItem.getOrderNumber())){
                        throw new RuntimeException("客户订单号不能为空");
                    }else{
                        //判断客户订单是否存在
                        custOrderId = StringUtil.obj2Str(orderOneItem.getOrderNumber());
                        suffix = "20"+custOrderId.substring(0,4);
                        //查询正表
                        customerOrderInfoMap = invoiceInfoMapper.selectCustomerOrderById(custOrderId);
                        if(StringUtil.isEmptyOrNull(customerOrderInfoMap)){
                            //查询历史表
                            Map<String,String> selectMap = new HashMap<String,String>();
                            selectMap.put("suffix",suffix);
                            selectMap.put("custOrderId",custOrderId);
                            customerOrderInfoMap = invoiceInfoMapper.selectCustomerOrderByIdHis(selectMap);
                            if(StringUtil.isNotEmptyOrNull(customerOrderInfoMap)){
                                hisFlag = true;
                            }else{
                                throw new RuntimeException("该客户订单不存在，请核查客户订单号是否正确");
                            }

                        }else{
                            hisFlag = false;
                        }
                    }
                    String prodInstId = "";
                    if(StringUtil.isNotEmptyOrNull(orderOneItem.getProductId())){
                        prodInstId = StringUtil.obj2Str(orderOneItem.getProductId());
                    }else{
                        throw new RuntimeException("营业订单费用对应的产品实例的id不能为空，请核查");
                    }
                    //通过接入类产品实例获取接入类产品号码信息
                    Map<String,String> prodInstAccNbrMap0 = new HashMap<String,String>();
                    prodInstAccNbrMap0.put("prodInstId",prodInstId);
                    Map<String,String> prodInstAccNbrMap = qryCustInfoMapper.selectProdInstAccNbrById(prodInstAccNbrMap0);
                    if(StringUtil.isNotEmptyOrNull(prodInstAccNbrMap)){
                        inMap.put("accNbr",StringUtil.obj2Str(prodInstAccNbrMap.get("ACC_NBR")));
                        inMap.put("accNbrTypeCd",StringUtil.obj2Str(prodInstAccNbrMap.get("ACC_NBR_TYPE_CD")));
                        inMap.put("prodInstId",StringUtil.obj2Str(prodInstAccNbrMap.get("PROD_INST_ID")));
                        inMap.put("lanId",StringUtil.obj2Str(prodInstAccNbrMap.get("LAN_ID")));
                    }else{
                        throw new RuntimeException("营业订单费用对应的产品实例的id不正确，请核查");
                    }
                    //通过接入类产品实例ID获取客户ID
                    Map<String,String> custInfoMap = invoiceInfoMapper.getCustIdByProdInstId(prodInstId);
                    if(StringUtil.isNotEmptyOrNull(custInfoMap)){
                        inMap.put("custId",StringUtil.obj2Str(custInfoMap.get("CUSTID")));
                        inMap.put("custName",StringUtil.obj2Str(custInfoMap.get("PARTYNAME")));
                    }else{
                        throw new RuntimeException("查询客户ID出错");
                    }

                    //通过接入类产品实例ID获取产品ID
                    String productId0 = invoiceInfoMapper.getProductIdById(prodInstId);
                    if(StringUtil.isNotEmptyOrNull(productId0)){
                        String productId = StringUtil.obj2Str(productId0);
                        inMap.put("productId",productId);
                        //通过productId 获取公司类型
                        int a = invoiceInfoMapper.qryProductType(productId);
                        if(a>0){
                            //股份公司
                            inMap.put("company","1");
                        }else{
                            //固网公司
                            inMap.put("company","2");
                        }
                    }else {
                        throw new RuntimeException("该客户订单对应的号码不存在");
                    }
                    //判断该客户订单是否可以打印电子发票——获取费用项信息
                    List<Map<String,String>> acctItemList = new ArrayList<Map<String,String>>();
                    int acctItemListSize = 0;
                    inMap.put("custOrderId",custOrderId);
                    //查询正表
                    if(!hisFlag){
                        //查询 invoice_id 是空的情况下，是否存在可以打印的发票信息
                        acctItemList = invoiceInfoMapper.selectInvoiceBycustOderId(inMap);
                        acctItemListSize = acctItemList.size();
                        if(acctItemListSize==0){
                            //查询 invoice_id 不为空的情况下，判断该发票是否已经作废
                            List<Map<String,String>> invoiceIdList = invoiceInfoMapper.selectInvoiceIdByCustOrderId(inMap);
                            int invoiceIdListSize = invoiceIdList.size();
                            if(invoiceIdListSize>0){
                                for(int d=0;d<invoiceIdListSize;d++){
                                    if(StringUtil.isNotEmptyOrNull(invoiceIdList.get(d).get("INVOICEID"))){
                                        String invoiceId = StringUtil.obj2Str(invoiceIdList.get(d).get("INVOICEID"));
                                        //查询发票作废信息表
                                        List<Map<String,String>> statusCdList = invoiceInfoMapper.selectInvoiceStatusCdByInvoiceId(invoiceId);
                                        int statusCdListSize = statusCdList.size();
                                        if(statusCdListSize>0){
                                            //该电子发票已经失效，可以继续打印
                                            inMap.put("failure","true");
                                            acctItemList = invoiceInfoMapper.selectInvoiceBycustOderId(inMap);
                                            acctItemListSize = acctItemList.size();
                                        }else{
                                            throw new RuntimeException("该客户订单已经打印过电子发票");
                                        }
                                    }
                                }
                            }
                        }
                    }else{
                        //查询历史表
                        inMap.put("suffix",suffix);
                        acctItemList = invoiceInfoMapper.selectInvoiceBycustOderId(inMap);
                        acctItemListSize = acctItemList.size();
                        if(acctItemListSize==0){
                            //判断该发票是否已经作废
                            List<Map<String,String>> invoiceIdList = invoiceInfoMapper.selectInvoiceIdByCustOrderIdHis(inMap);
                            int invoiceIdListSize = invoiceIdList.size();
                            if(invoiceIdListSize>0){
                                for(int d=0;d<invoiceIdListSize;d++){
                                    if(StringUtil.isNotEmptyOrNull(invoiceIdList.get(d).get("INVOICEID"))){
                                        String invoiceId = StringUtil.obj2Str(invoiceIdList.get(d).get("INVOICEID"));
                                        //查询发票作废信息表
                                        List<Map<String,String>> statusCdList = invoiceInfoMapper.selectInvoiceStatusCdByInvoiceId(invoiceId);
                                        int statusCdListSize = statusCdList.size();
                                        if(statusCdListSize>0){
                                            //该电子发票已经失效，可以继续打印
                                            inMap.put("failure","true");
                                            acctItemList = invoiceInfoMapper.selectInvoiceBycustOderId(inMap);
                                            acctItemListSize = acctItemList.size();
                                        }else{
                                            throw new RuntimeException("该客户订单已经打印过电子发票");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if(acctItemListSize>0){
                        String systemUserCode = "";
                        String channelId = "";
                        if(StringUtil.isNotEmptyOrNull(headers.getFirst("Staff-Code"))){
                            String staffId = StringUtil.obj2Str(headers.getFirst("Staff-Code"));
                            //查询开票工号信息
                            Map<String,String> sysCodeMap = invoiceInfoMapper.getSysCodeInfoByStaffId(staffId);
                            if(StringUtil.isNotEmptyOrNull(sysCodeMap)){
                                systemUserCode = StringUtil.obj2Str(sysCodeMap.get("SYSTEMUSERCODE"));
                                channelId = StringUtil.obj2Str(sysCodeMap.get("CHANNELID"));
                            }else{
                                systemUserCode = StringUtil.obj2Str(acctItemList.get(0).get("SYSTEMUSERCODE"));
                                channelId = StringUtil.obj2Str(acctItemList.get(0).get("CHANNELID"));
                            }
                        }else{
                            systemUserCode = StringUtil.obj2Str(acctItemList.get(0).get("SYSTEMUSERCODE"));
                            channelId = StringUtil.obj2Str(acctItemList.get(0).get("CHANNELID"));
                        }

                        String notes = StringUtil.obj2Str(acctItemList.get(0).get("NOTES"));
                        String skrLanId = StringUtil.obj2Str(acctItemList.get(0).get("LANID"));
                        String busiAccept = StringUtil.obj2Str(acctItemList.get(0).get("BUSIACCEPT"));
                        inMap.put("systemUserCode",systemUserCode);
                        inMap.put("channelId",channelId);
                        inMap.put("notes",notes);
                        inMap.put("skrLanId",skrLanId);
                        inMap.put("busiAccept",busiAccept);

                        //获取收款人信息
                        String skrName = "";
                        if(StringUtil.isNotEmptyOrNull(acctItemList.get(0).get("SYSTEMUSERCODE"))){
                            String skrStaffCode = StringUtil.obj2Str(acctItemList.get(0).get("SYSTEMUSERCODE"));
                            inMap.put("skrStaffCode",skrStaffCode);
                            skrName = invoiceInfoMapper.getStaffName(skrStaffCode);
                        }
                        inMap.put("skrName",skrName);
                        String skrChannelId = "";
                        if(StringUtil.isNotEmptyOrNull(acctItemList.get(0).get("CHANNELID"))){
                            skrChannelId = StringUtil.obj2Str(acctItemList.get(0).get("CHANNELID"));
                        }
                        inMap.put("skrChannelId",skrChannelId);
                        //获取购买方信息
                        Map<String,String> gmfMap = doQryGmfInfo(StringUtil.obj2Str(inMap.get("custId")));
                        inMap.put("addsPhone",StringUtil.obj2Str(gmfMap.get("addsPhone")));
                        inMap.put("GMF_YHZH",StringUtil.obj2Str(gmfMap.get("GMF_YHZH")));
                        inMap.put("GMF_NSRSBH",StringUtil.obj2Str(gmfMap.get("GMF_NSRSBH")));
                        inMap.put("telPhone",StringUtil.obj2Str(gmfMap.get("telPhone")));

                        //通过号码查询开票信息
                        List<Map<String,String>> invoiceInfoList = invoiceInfoMapper.selectInvoiceInfo(StringUtil.obj2Str(inMap.get("accNbr")));
                        InvoiceInformation invoiceInformation = getInvoiceInformation(invoiceInfoList);
                        if(StringUtil.isEmptyOrNull(invoiceInformation.geteMail())){
                            //默认有189邮箱
                            String mailBox = StringUtil.obj2Str(inMap.get("telPhone")) + "@189.cn";
                            if(StringUtil.isEmptyOrNull(mailBox)){
                                String custId = inMap.get("custId");
                                String accNbr4 = invoiceInfoMapper.selectAccNbrByCustId(custId);
                                mailBox = accNbr4+"@189.cn";
                            }
                            inMap.put("email",mailBox);
                        }else{
                            inMap.put("email",StringUtil.obj2Str(invoiceInformation.geteMail()));
                        }

                        //调用中兴接口进行电子发票的打印
                        Map<String,String> outMap = doDealInvoiceOpenInfo(issueInvoice, inMap, acctItemList);
                        //调用成功后数据落表
                        String returnCode = StringUtil.obj2Str(outMap.get("returnCode"));
                        if(StringUtil.isEqual("0000",returnCode)){
                            String date = DateUtil.toStringYmdHms(new Date());
                            Date createDate = DateUtil.toDateYmdHms(date);
                            //落发票开具信息表——electInvoiceIssuDetail
                            Long electInvoiceIssuDtlId = commonMapper.getSeq("SEQ_ELECT_INVOIISSU_DTL_ID");
                            Long electInvoiceItemsBatchId = commonMapper.getSeq("SEQ_ELECT_INVO_ITEMS_BATCHID");
                            ElectInvoiceIssuDetail electInvoiceIssuDetail = new ElectInvoiceIssuDetail();
                            electInvoiceIssuDetail.setElectInvoiissuDtlId(electInvoiceIssuDtlId);
                            electInvoiceIssuDetail.setElectInvoiceItemsBatchId(electInvoiceItemsBatchId);
                            electInvoiceIssuDetail.setCreateTime(createDate);
                            electInvoiceIssuDetail.setBusiDate(NumberUtil.intValueOf(DateUtil.toStringYmd(new Date())));
                            electInvoiceIssuDetail.setSystemUserCode(StringUtil.obj2Str(inMap.get("systemUserCode")));
                            electInvoiceIssuDetail.setOrderid("CRM"+StringUtil.obj2Str(inMap.get("busiAccept")));
                            electInvoiceIssuDetail.setReceipttype("1");
                            electInvoiceIssuDetail.setFptsfs("02");  //需要核对
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getInvoiceHeader())){
                                electInvoiceIssuDetail.setGmfMc(issueInvoice.getInvoiceHeader());
                            }else{
                                electInvoiceIssuDetail.setGmfMc(StringUtil.obj2Str(inMap.get("custName")));
                            }

                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBuyerAddress())){
                                electInvoiceIssuDetail.setGmfDzdh(StringUtil.obj2Str(issueInvoice.getBuyerAddress()));
                            }else{
                                electInvoiceIssuDetail.setGmfDzdh(StringUtil.obj2Str(inMap.get("addsPhone")));
                            }
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBackAccount())){
                                electInvoiceIssuDetail.setGmfYhzh(StringUtil.obj2Str(issueInvoice.getBackAccount()));
                            }else{
                                electInvoiceIssuDetail.setGmfYhzh(StringUtil.obj2Str(inMap.get("GMF_YHZH")));
                            }
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBuyerTaxPayerId())){
                                electInvoiceIssuDetail.setGmfNsrsbh(issueInvoice.getBuyerTaxPayerId());
                            }else{
                                electInvoiceIssuDetail.setGmfNsrsbh(inMap.get("GMF_NSRSBH"));
                            }
                            electInvoiceIssuDetail.setGmfJfhm(StringUtil.obj2Str(inMap.get("accNbr")));
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBuyerPhone())){
                                electInvoiceIssuDetail.setGmfSjh(StringUtil.obj2Str(issueInvoice.getBuyerPhone()));
                            }else{
                                electInvoiceIssuDetail.setGmfSjh(StringUtil.obj2Str(inMap.get("telPhone")));
                            }
                            electInvoiceIssuDetail.setGmfDzyx(StringUtil.obj2Str(inMap.get("email")));
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getPayee())){
                                electInvoiceIssuDetail.setSkr(StringUtil.obj2Str(issueInvoice.getPayee()));
                            }else{
                                electInvoiceIssuDetail.setSkr(StringUtil.obj2Str(inMap.get("skrName")));
                            }
                            electInvoiceIssuDetail.setSkrLanId(StringUtil.obj2Str(inMap.get("skrLanId")));
                            electInvoiceIssuDetail.setBz(StringUtil.obj2Str(inMap.get("notes")));
                            electInvoiceIssuDetail.setBy1(StringUtil.obj2Str(inMap.get("company")));
                            electInvoiceIssuDetail.setStatusCd("0");
                            electInvoiceIssuDetail.setRevertFlag("0");
                            electInvoiceIssuDetail.setAccNbrTypeCd(NumberUtil.intValueOf(inMap.get("accNbrTypeCd")));
                            electInvoiceIssuDetail.setPrintCount(0);
                            electInvoiceIssuDetail.setPrintFlag("1");
                            electInvoiceIssuDetail.setInvoiceType("100");
                            electInvoiceIssuDetail.setStatusTime(createDate);
                            electInvoiceIssuDetailMapper.insert(electInvoiceIssuDetail);

                            //落发票开具费用明细表
                            for (int i = 0; i < acctItemList.size(); i++) {
                                ElectInvoIssuItemsDetail electInvoIssuItemsDetail = new ElectInvoIssuItemsDetail();
                                Long ELECT_INVO_ISSU_ITEMS_DTL_ID = commonMapper.getSeq("SEQ_ELECTINVO_ISSU_ITEMS_DTLID");
                                Integer busiDate = NumberUtil.intParseInt(DateUtil.toStringYmd(new Date()));
                                electInvoIssuItemsDetail.setElectInvoIssuItemsDtlId(ELECT_INVO_ISSU_ITEMS_DTL_ID);
                                electInvoIssuItemsDetail.setElectInvoiceItemsBatchId(electInvoiceItemsBatchId);
                                electInvoIssuItemsDetail.setCreateTime(createDate);
                                electInvoIssuItemsDetail.setBusiDate(busiDate);
                                electInvoIssuItemsDetail.setFphxz("0"); //费用行性质
                                electInvoIssuItemsDetail.setItemname(acctItemList.get(i).get("ACCTITEMNAME")); //费用项名称
                                electInvoIssuItemsDetail.setItemid(StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMID")));   //费用项id
                                electInvoIssuItemsDetail.setDw("元");//计量单位
                                electInvoIssuItemsDetail.setXmsl("1");  //项目数量
                                electInvoIssuItemsDetail.setXmdj(StringUtil.obj2Str(acctItemList.get(i).get("PRICE")));//项目单价
                                electInvoIssuItemsDetail.setJeType("2"); //金额类型 1:项目金额不含税 2:项目金额含税
                                electInvoIssuItemsDetail.setXmje(StringUtil.obj2Str(acctItemList.get(i).get("AMOUNT")));  //项目金额
                                electInvoIssuItemsDetail.setSl(StringUtil.obj2Str(acctItemList.get(i).get("TAXRATE")));  //税率
                                electInvoIssuItemsDetail.setSe(StringUtil.obj2Str(acctItemList.get(i).get("TAX")));  //税额
                                electInvoIssuItemsDetailMapper.insert(electInvoIssuItemsDetail);

                                //电子发票明细表
                                ElectInvoiceDetail electInvoiceDetail = new ElectInvoiceDetail();
                                Long electInvoiceDetailId = commonMapper.getSeq("SEQ_ELECT_INVOICE_DETAIL_ID");
                                electInvoiceDetail.setElectInvoiceDetailId(NumberUtil.getLongFromObj(electInvoiceDetailId));
                                electInvoiceDetail.setElectInvoiissuDtlId(NumberUtil.getLongFromObj(electInvoiceIssuDtlId));
                                electInvoiceDetail.setCreateTime(createDate);
                                electInvoiceDetail.setBusiDate(busiDate);
                                electInvoiceDetail.setAcctItemId(NumberUtil.getLongFromObj(StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMID"))));
                                electInvoiceDetail.setAcctItemTypeId(NumberUtil.getIntFromObj(acctItemList.get(i).get("ACCTITEMTYPEID")));
                                electInvoiceDetail.setAcctItemTypeName(StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMNAME")));
                                electInvoiceDetail.setAmount(StringUtil.obj2Str(acctItemList.get(i).get("AMOUNT")));
                                electInvoiceDetail.setLanId(NumberUtil.getLongFromObj(acctItemList.get(i).get("LANID")));
                                electInvoiceDetail.setAccNbr(StringUtil.obj2Str(acctItemList.get(i).get("ACCNBR")));
                                electInvoiceDetail.setSystemUserCode(systemUserCode);
                                electInvoiceDetail.setBusiAccept(NumberUtil.getLongFromObj(acctItemList.get(i).get("BUSIACCEPT")));
                                electInvoiceDetail.setStatusCd("0");
                                electInvoiceDetail.setStatusDate(createDate);
                                electInvoiceDetailMapper.insert(electInvoiceDetail);

                                //更新acct_item表的invoiceid及bak2字段，标记为电子发票
                                if(hisFlag){
                                    Map<String,String> updateMap = new HashMap<String,String>();
                                    updateMap.put("suffix",suffix);
                                    updateMap.put("type","2");
                                    updateMap.put("invoiceId",StringUtil.obj2Str(electInvoiceIssuDtlId));
                                    updateMap.put("acctItemId",StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMID")));
                                    invoiceInfoMapper.updateAcctItemInvoiceIdHis(updateMap);
                                }else{
                                    Map<String,String> updateMap = new HashMap<String,String>();
                                    updateMap.put("type","2");
                                    updateMap.put("invoiceId",StringUtil.obj2Str(electInvoiceIssuDtlId));
                                    updateMap.put("acctItemId",StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMID")));
                                    invoiceInfoMapper.updateAcctItemInvoiceId(updateMap);
                                }
                            }
                        }else{
                            //调中兴接口失败，落数据
                            String date = DateUtil.toStringYmdHms(new Date());
                            Date createDate = DateUtil.toDateYmdHms(date);
                            //落发票开具信息表——electInvoiceIssuDetail
                            Long electInvoiceIssuDtlId = commonMapper.getSeq("SEQ_ELECT_INVOIISSU_DTL_ID");
                            Long electInvoiceItemsBatchId = commonMapper.getSeq("SEQ_ELECT_INVO_ITEMS_BATCHID");
                            ElectInvoiceIssuDetail electInvoiceIssuDetail = new ElectInvoiceIssuDetail();
                            electInvoiceIssuDetail.setElectInvoiissuDtlId(electInvoiceIssuDtlId);
                            electInvoiceIssuDetail.setElectInvoiceItemsBatchId(electInvoiceItemsBatchId);
                            electInvoiceIssuDetail.setCreateTime(createDate);
                            electInvoiceIssuDetail.setBusiDate(NumberUtil.intValueOf(DateUtil.toStringYmd(new Date())));
                            electInvoiceIssuDetail.setSystemUserCode(StringUtil.obj2Str(inMap.get("systemUserCode")));
                            electInvoiceIssuDetail.setOrderid("CRM"+StringUtil.obj2Str(inMap.get("busiAccept")));
                            electInvoiceIssuDetail.setReceipttype("1");
                            electInvoiceIssuDetail.setFptsfs("02");  //需要核对
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getInvoiceHeader())){
                                electInvoiceIssuDetail.setGmfMc(issueInvoice.getInvoiceHeader());
                            }else{
                                electInvoiceIssuDetail.setGmfMc(StringUtil.obj2Str(inMap.get("custName")));
                            }
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBuyerAddress())){
                                electInvoiceIssuDetail.setGmfDzdh(StringUtil.obj2Str(issueInvoice.getBuyerAddress()));
                            }else{
                                electInvoiceIssuDetail.setGmfDzdh(StringUtil.obj2Str(inMap.get("addsPhone")));
                            }
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBackAccount())){
                                electInvoiceIssuDetail.setGmfYhzh(StringUtil.obj2Str(issueInvoice.getBackAccount()));
                            }else{
                                electInvoiceIssuDetail.setGmfYhzh(StringUtil.obj2Str(inMap.get("GMF_YHZH")));
                            }
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBuyerTaxPayerId())){
                                electInvoiceIssuDetail.setGmfNsrsbh(issueInvoice.getBuyerTaxPayerId());
                            }else{
                                electInvoiceIssuDetail.setGmfNsrsbh(inMap.get("GMF_NSRSBH"));
                            }
                            electInvoiceIssuDetail.setGmfJfhm(StringUtil.obj2Str(inMap.get("accNbr")));
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getBuyerPhone())){
                                electInvoiceIssuDetail.setGmfSjh(StringUtil.obj2Str(issueInvoice.getBuyerPhone()));
                            }else{
                                electInvoiceIssuDetail.setGmfSjh(StringUtil.obj2Str(inMap.get("telPhone")));
                            }
                            electInvoiceIssuDetail.setGmfDzyx(StringUtil.obj2Str(inMap.get("email")));
                            if(StringUtil.isNotEmptyOrNull(issueInvoice.getPayee())){
                                electInvoiceIssuDetail.setSkr(StringUtil.obj2Str(issueInvoice.getPayee()));
                            }else{
                                electInvoiceIssuDetail.setSkr(StringUtil.obj2Str(inMap.get("skrName")));
                            }
                            electInvoiceIssuDetail.setSkrLanId(StringUtil.obj2Str(inMap.get("skrLanId")));
                            electInvoiceIssuDetail.setBz("发票开具接口调用失败");
                            electInvoiceIssuDetail.setBy1(StringUtil.obj2Str(inMap.get("company")));
                            electInvoiceIssuDetail.setStatusCd("3");
                            electInvoiceIssuDetail.setRevertFlag("0");
                            electInvoiceIssuDetail.setAccNbrTypeCd(NumberUtil.intValueOf(inMap.get("accNbrTypeCd")));
                            electInvoiceIssuDetail.setStatusTime(createDate);
                            electInvoiceIssuDetailMapper.insert(electInvoiceIssuDetail);

                            for (int i = 0; i < acctItemList.size(); i++) {
                                ElectInvoIssuItemsDetail electInvoIssuItemsDetail = new ElectInvoIssuItemsDetail();
                                Long ELECT_INVO_ISSU_ITEMS_DTL_ID = commonMapper.getSeq("SEQ_ELECTINVO_ISSU_ITEMS_DTLID");
                                Integer busiDate = NumberUtil.intParseInt(DateUtil.toStringYmd(new Date()));
                                electInvoIssuItemsDetail.setElectInvoIssuItemsDtlId(ELECT_INVO_ISSU_ITEMS_DTL_ID);
                                electInvoIssuItemsDetail.setElectInvoiceItemsBatchId(electInvoiceItemsBatchId);
                                electInvoIssuItemsDetail.setCreateTime(createDate);
                                electInvoIssuItemsDetail.setBusiDate(busiDate);
                                electInvoIssuItemsDetail.setFphxz("0"); //费用行性质
                                electInvoIssuItemsDetail.setItemname(acctItemList.get(i).get("ACCTITEMNAME")); //费用项名称
                                electInvoIssuItemsDetail.setItemid(StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMID")));   //费用项id
                                electInvoIssuItemsDetail.setDw("元");//计量单位
                                electInvoIssuItemsDetail.setXmsl("1");  //项目数量
                                electInvoIssuItemsDetail.setXmdj(StringUtil.obj2Str(acctItemList.get(i).get("PRICE")));//项目单价
                                electInvoIssuItemsDetail.setJeType("2"); //金额类型 1:项目金额不含税 2:项目金额含税
                                electInvoIssuItemsDetail.setXmje(StringUtil.obj2Str(acctItemList.get(i).get("AMOUNT")));  //项目金额
                                electInvoIssuItemsDetail.setSl(StringUtil.obj2Str(acctItemList.get(i).get("TAXRATE")));  //税率
                                electInvoIssuItemsDetail.setSe(StringUtil.obj2Str(acctItemList.get(i).get("TAX")));  //税额
                                electInvoIssuItemsDetailMapper.insert(electInvoIssuItemsDetail);

                                //电子发票明细表
                                ElectInvoiceDetail electInvoiceDetail = new ElectInvoiceDetail();
                                Long electInvoiceDetailId = commonMapper.getSeq("SEQ_ELECT_INVOICE_DETAIL_ID");
                                electInvoiceDetail.setElectInvoiceDetailId(NumberUtil.getLongFromObj(electInvoiceDetailId));
                                electInvoiceDetail.setElectInvoiissuDtlId(NumberUtil.getLongFromObj(electInvoiceIssuDtlId));
                                electInvoiceDetail.setCreateTime(createDate);
                                electInvoiceDetail.setBusiDate(busiDate);
                                electInvoiceDetail.setAcctItemId(NumberUtil.getLongFromObj(StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMID"))));
                                electInvoiceDetail.setAcctItemTypeId(NumberUtil.getIntFromObj(acctItemList.get(i).get("ACCTITEMTYPEID")));
                                electInvoiceDetail.setAcctItemTypeName(StringUtil.obj2Str(acctItemList.get(i).get("ACCTITEMNAME")));
                                electInvoiceDetail.setAmount(StringUtil.obj2Str(acctItemList.get(i).get("AMOUNT")));
                                electInvoiceDetail.setLanId(NumberUtil.getLongFromObj(acctItemList.get(i).get("LANID")));
                                electInvoiceDetail.setAccNbr(StringUtil.obj2Str(acctItemList.get(i).get("ACCNBR")));
                                electInvoiceDetail.setSystemUserCode(systemUserCode);
                                electInvoiceDetail.setBusiAccept(NumberUtil.getLongFromObj(acctItemList.get(i).get("BUSIACCEPT")));
                                electInvoiceDetail.setStatusCd("3");
                                electInvoiceDetail.setStatusDate(createDate);
                                electInvoiceDetailMapper.insert(electInvoiceDetail);
                            }
                            String ResultMsg = StringUtil.obj2Str(outMap.get("ResultMsg"));
                            throw new RuntimeException(ResultMsg);
                        }
                    }else{
                        throw new RuntimeException("该客户订单未产生费用，不可以打印电子发票");
                    }
                }
            }else{
                throw new RuntimeException("营业订单费用不能为空");
            }
        }else{
            doIssueInvoiceByBill(issueInvoice,headers);
        }
    }

    /**
     * 调计费侧接口开具电子发票
     * @param issueInvoice
     * @param headers
     * @return
     */
   public void doIssueInvoiceByBill(IssueInvoice issueInvoice,HttpHeaders headers){

       String paraId = "1";
       String url = invoiceInfoMapper.getInterfaceBill(paraId);
       //String url = "http://137.64.44.248:10010/esbWS/rest/sGrpEPrint";
       //设置请求体信息
       HttpEntity<IssueInvoice> requestEntity = new HttpEntity<IssueInvoice>(issueInvoice, headers);
       //发送请求
       RestTemplate restTemplate = new RestTemplate();
       ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
       String resMsg = response.getBody();
       //{"SVC_ERR_MSG32":"Not Implemented","SVC_ERR_NO32":"501"}
       ObjectMapper objectMapper = new ObjectMapper();
       Map<String,String> map = new HashMap<String,String>();
       try {
           map = objectMapper.readValue(resMsg,Map.class);
       } catch (IOException e) {
           e.printStackTrace();
       }
       String resultCode = StringUtil.obj2Str(map.get("SVC_ERR_NO32"));
       String resultMsg = StringUtil.obj2Str(map.get("SVC_ERR_MSG32"));
       if(!StringUtil.isEqual(resultCode,"200")){
           throw new RuntimeException(resultMsg);
       }
       log.info(resMsg);
   }


    /**
     * 调用中兴接口进行电子发票的打印
     * @param issueInvoice
     * @param inMap
     * @param acctItemList
     * @return
     */
    public Map<String, String> doDealInvoiceOpenInfo(IssueInvoice issueInvoice,Map inMap,List<Map<String,String>> acctItemList){

        MapBean mapBean = new MapBean();
        mapBean.setValue("ContractRoot.TcpCont.sysId", "001");  //系统ID 001 CRM  002 计费 003 网厅 004 爱营销
        mapBean.setValue("ContractRoot.TcpCont.interfaceCode", "FPKJ001");  //接口服务编码
        mapBean.setValue("ContractRoot.TcpCont.pssWd", "001"); //业务系统调用服务校验密码
        Date date = new Date();
        mapBean.setValue("ContractRoot.TcpCont.requestTime", DateUtil.toStringYmdHms(date));

        String str = "ContractRoot.SvcCont.content.Request_FPKJ001.";
        //订单ID，CRM+业务流水号
        mapBean.setValue(str + "OrderId", "CRM"+StringUtil.obj2Str(inMap.get("busiAccept")));
        //1 实时开具 2 非实时开具 --默认传1
        mapBean.setValue(str + "ReceiptType", "1");
        //编码表版本号
        mapBean.setValue(str + "BMB_BBH", "");
        //征税方式:  0：普通征税 2：差额征税
        mapBean.setValue(str + "ZSFS", "0");
        //电子发票推送方式  01 短信 02 邮箱 03短信&邮箱
        mapBean.setValue(str + "FPTSFS", "02");
        //购买方纳税人识别号  般纳税人传，普通客户传空,但是一般纳税人不打印电子发票
        //获取纳税人识别号
        if(StringUtil.isEmptyOrNull(issueInvoice.getBuyerTaxPayerId())){
            mapBean.setValue(str + "GMF_NSRSBH",inMap.get("GMF_NSRSBH"));
        }else{
            mapBean.setValue(str + "GMF_NSRSBH",issueInvoice.getBuyerTaxPayerId());
        }
        //购买方名称
        if(StringUtil.isEmptyOrNull(StringUtil.obj2Str(issueInvoice.getInvoiceHeader()))){
            mapBean.setValue(str + "GMF_MC",inMap.get("custName"));
        }else{
            mapBean.setValue(str + "GMF_MC",StringUtil.obj2Str(issueInvoice.getInvoiceHeader()));
        }
        //购买方缴费号码
        mapBean.setValue(str + "GMF_JFHM", StringUtil.obj2Str(inMap.get("accNbr")));
        //客户联系地址   可为空 买方地址、电话,同时传电话地址使用英文“;”分隔，如：“江西南昌市***路**号;18901928342”。--客户联系地址
        if(StringUtil.isEmptyOrNull(issueInvoice.getBuyerAddress())){
            mapBean.setValue(str + "GMF_DZDH",StringUtil.obj2Str(inMap.get("addsPhone")));
        }else{
            mapBean.setValue(str + "GMF_DZDH",StringUtil.obj2Str(issueInvoice.getBuyerAddress())); //证件地址+电话 20170612  //和GMF_JFHM都是缴费号码  和wuxb确定  20160823
        }
        //购买方银行账号
        //获取税务开户银行名称+账号
        if(StringUtil.isEmptyOrNull(issueInvoice.getBackAccount())){
            mapBean.setValue(str + "GMF_YHZH", StringUtil.obj2Str(inMap.get("GMF_YHZH")));
        }else{
            mapBean.setValue(str + "GMF_YHZH", StringUtil.obj2Str(issueInvoice.getBackAccount()));
        }
        //购买方手机号
        if(StringUtil.isEmptyOrNull(issueInvoice.getBuyerPhone())){
            mapBean.setValue(str + "GMF_SJH", StringUtil.obj2Str(inMap.get("telPhone")));
        }else{
            mapBean.setValue(str + "GMF_SJH", StringUtil.obj2Str(issueInvoice.getBuyerPhone()));
        }
        //购买方电子邮箱
        mapBean.setValue(str + "GMF_DZYX", StringUtil.obj2Str(inMap.get("email")));
        //收款人
        if(StringUtil.isNotEmptyOrNull(issueInvoice.getPayee())){
            mapBean.setValue(str + "SKR", StringUtil.obj2Str(issueInvoice.getPayee()));
        }else{
            mapBean.setValue(str + "SKR", StringUtil.obj2Str(inMap.get("skrName")));
        }
        //收款人归属
        mapBean.setValue(str + "SKR_LAN_ID", StringUtil.obj2Str(inMap.get("skrLanId")));
        //扣除额
        mapBean.setValue(str + "KCE", "");

        //String channelId = StringUtil.obj2Str(inMap.get("channelId"));
        //电信开票点编码
        mapBean.setValue(str + "DX_KPD_BM","dqwt01");
        //复核人，传复核人工号ID—传空
        if(StringUtil.isNotEmptyOrNull(issueInvoice.getReviewer())){
            mapBean.setValue(str + "FHR", StringUtil.obj2Str(issueInvoice.getReviewer()));
        }else{
            String fhrName = "";
            String channelType = "";
            if(StringUtil.isNotEmptyOrNull(inMap.get("skrStaffCode"))){
                String skrStaffCode = StringUtil.obj2Str(inMap.get("skrStaffCode"));
                channelType = StringUtil.obj2Str(invoiceInfoMapper.qryChannelType(StringUtil.obj2Str(inMap.get("skrChannelId"))));
                // 查询 channle_view表，如果channel_type_cd = 1000 则表示自有渠道
                if(StringUtil.isNotEmptyOrNull(channelType) && StringUtil.isEqual("1000",channelType)){
                    fhrName = StringUtil.obj2Str(invoiceInfoMapper.qryFhrStaffName(skrStaffCode));
                }
            }
            mapBean.setValue(str + "FHR", fhrName);
        }

        //电信开票点名称  也就是channelName  营业厅名称
        //获取渠道名称
        //String channelName = invoiceInfoMapper.getChannelName(channelId);
        mapBean.setValue(str + "DX_KPD_NAME", "集团电渠网厅/欢GO");
        //电信数据来源编码
        /**1 	营业厅 	YY
         2 	网厅 	WT
         3 	掌厅 	ZT
         4 	手工开票 	SG
         5 	自助终端 	ZZ
         6 	第三方平台 	SF
         7 	PDA （浙江外线人员上门开具）PD
         8 	天翼生活 APP 	TY
         9 	易销售 	YX
         */
        mapBean.setValue(str + "DX_SJLY_BM", "WT");

        String provinceCode="8140000";
        mapBean.setValue(str + "PROVINCE_CODE", provinceCode);
        //地市编码
        mapBean.setValue(str + "CITY_CODE", StringUtil.obj2Str(issueInvoice.getLanId()));
        //备注
        mapBean.setValue(str + "BZ", StringUtil.obj2Str(inMap.get("notes")));
        //备用字段   1 股份公司 2 固网公司
        mapBean.setValue(str + "BY1",StringUtil.obj2Str(inMap.get("company")));

        //费用项列表
        String str1 = "ContractRoot.SvcCont.content.Request_FPKJ001.FeeItemList.FeeItem";
      //  List<Map<String, String>> feeItemList =acctItemList;
        for(int i=0; i<acctItemList.size(); i++){
            Map<String, String> feeItem = acctItemList.get(i);
            //费用行性质 0正常行、 2 被折扣行 为空是默认是正常行
            mapBean.setValue(str1+"[" + i + "].FPHXZ",0);
            //查询商品编码
            String paraId = StringUtil.obj2Str(feeItem.get("ACCTITEMTYPEID"));
            String shpCode = invoiceInfoMapper.getShopCode(paraId);
            if(StringUtil.isNotEmptyOrNull(shpCode)){
                //商品编码
                mapBean.setValue(str1+"[" + i + "].SPBM",shpCode);
            }else{
                //商品编码
                mapBean.setValue(str1+"[" + i + "].SPBM","3030000000000000000");  //默认通信服务费
            }

            //自行编码
            mapBean.setValue(str1+"[" + i + "].ZXBM","");
            //优惠政策标识  0：不使用，1：使用
            mapBean.setValue(str1+"[" + i + "].YHZCBS","0");
            //零税率标识  1 出口免税和其他免税优惠政策（免税）2 不征增值税（不征税）3	普通零税率（0%）4	当使用优惠政策，必填
            mapBean.setValue(str1+"[" + i + "].LSLBS","3");
            //增值税特殊管理
            mapBean.setValue(str1+"[" + i + "].ZZSTSGL","");
            //费用项名称
            mapBean.setValue(str1 + "[" + i + "].ItemName", StringUtil.obj2Str(feeItem.get("ACCTITEMNAME")));
            //费用项id
            mapBean.setValue(str1 + "[" + i + "].ItemId", StringUtil.obj2Str(feeItem.get("ACCTITEMTYPEID")));
            //计量单位
            mapBean.setValue(str1 + "[" + i + "].DW", "元");
            //规格型号
            mapBean.setValue(str1 + "[" + i + "].GGXH", "");
            //项目数量
            mapBean.setValue(str1 + "[" + i + "].XMSL", "1");
            //项目单价
            mapBean.setValue(str1 + "[" + i + "].XMDJ", StringUtil.obj2Str(feeItem.get("AMOUNT")));
            //金额类型     1:项目金额不含税 2:项目金额含税
            mapBean.setValue(str1 + "[" + i + "].JE_TYPE", "2");
            //项目金额
            mapBean.setValue(str1 + "[" + i + "].XMJE", StringUtil.obj2Str(feeItem.get("AMOUNT")));
            //税率
            mapBean.setValue(str1 + "[" + i + "].SL", 0.00);
            //税额
            mapBean.setValue(str1 + "[" + i + "].SE", 0.00);
            //折扣率  费用行性质为被折扣行时必须
            mapBean.setValue(str1 + "[" + i + "].ZKL", "");
            //备用字段
            mapBean.setValue(str1 + "[" + i + "].FBY1", "");
            mapBean.setValue(str1 + "[" + i + "].FBY2", "");
            mapBean.setValue(str1 + "[" + i + "].FBY3", "");
            mapBean.setValue(str1 + "[" + i + "].FBY4", "");
            mapBean.setValue(str1 + "[" + i + "].FBY5", "");
        }

        String inXml0 = mapBean.toXML();
        log.info("===========调用中兴 发票开具接口 入参报文=========");
        log.info(inXml0);
        String inXml = MessageFormat.format(BusiConst.zxDoIssueInvoice, inXml0);
        String outXml = "";
        Map<String,String> reMap = new HashMap<String,String>();
        //接口地址配置起来
        String paraId = "3";
        String url = invoiceInfoMapper.getInterfaceBill(paraId);
        if(StringUtil.isEmptyOrNull(url)){
            throw new RuntimeException("未查询到开具发票接口的地址");
        }
        //String url = "http://137.64.43.94:10002/CSBProject/Proxy/CRM/Translator_crm_to_ZTE/TeleCRMtoZTE_FPKJ";
        MapBean resMapBean = new MapBean();
        try {
            outXml = wsClientService.doQryClientSerive(inXml, url);
            log.info("===========调用中兴 发票开具接口 返回报文=========");
            log.info(outXml);
            //保存接口调用记录
            resMapBean.fromXML(outXml);
            reMap.put("returnCode", StringUtil.obj2Str(resMapBean.getValue("ContractRoot.SvcCont.content.Response.returnCode")));
            reMap.put("ResultMsg", StringUtil.obj2Str(resMapBean.getValue("ContractRoot.SvcCont.content.Response.returnMessage")));
            if (StringUtil.isEqual("0000", resMapBean.getValue("ContractRoot.SvcCont.content.Response.returnCode "))) {
                log.debug("中兴发票开具接口 调用成功");
            }
        } catch (Exception e) {
            log.error("中兴发票开具接口 接口调用异常：" + e);
            reMap.put("returnCode", StringUtil.obj2Str(resMapBean.getValue("ContractRoot.SvcCont.content.Response.returnCode")));
            reMap.put("ResultMsg", "中兴发票开具接口 接口调用异常："+e.getMessage());
        }

//        try {
//            outXml = wsClientService.doQryClientSerive(inXml,url);
//            log.info("===========调用中兴 发票开具接口 返回报文=========");
//            log.info(outXml);
//            Document docOut = DocumentHelper.parseText(outXml);
//            Node returnCode0 = docOut.selectSingleNode("/ContractRoot/SvcCont/content/Response/returnCode");
//            Node returnMsg0 = docOut.selectSingleNode("/ContractRoot/SvcCont/content/Response/returnMessage");
//            String returnCode = StringUtil.obj2Str(returnCode0.getText().trim());
//            String returnMsg = StringUtil.obj2Str(returnMsg0.getText().trim());
//
//            reMap.put("returnCode", returnCode);
//            reMap.put("ResultMsg",returnMsg);
//            if (StringUtil.isEqual("0000",returnCode)) {
//                log.debug("中兴发票开具接口 调用成功");
//            }
//        } catch (Exception e) {
//            reMap.put("returnCode", "400");
//            reMap.put("ResultMsg","中兴发票开具接口 接口调用异常："+e.getMessage());
//        }
        return reMap;
   }

    //通过发票实例ID查询发票信息《发票接口》
    public Invoice getInvoiceById(String invoiceId,String lanId,HttpHeaders headers){

        Invoice invoice = new Invoice();
        String pattern = "[0-9]*";
        boolean isBill = Pattern.matches(pattern,invoiceId);
        if(isBill){
            //首先获取发票的价格
            List<Map<String,String>> amountList = invoiceInfoMapper.selectAmountList(invoiceId);
            int amountListSize = amountList.size();
            double amounts = 0;
            if(amountListSize>0){
                for(int i=0;i<amountListSize;i++){
                    double amount = NumberUtil.getDoubleFromObj(amountList.get(i).get("AMOUNT"));
                    amounts = amounts+amount;
                }
                //接口协议变更——所有的金额单位都是“分”
                amounts = amounts*100;
                String amountStr = StringUtil.obj2Str(amounts);
                invoice.setValue(amountStr);
                invoice.setType("1200");

                Map<String,String> invoiceMap = invoiceInfoMapper.selectInvoiceById(invoiceId);
                if(!StringUtils.isEmpty(invoiceMap)){
                    if(!StringUtils.isEmpty(invoiceMap.get("PDF_URL"))){
                        String url = String.valueOf(invoiceMap.get("PDF_URL"));
                        invoice.setUrl(url);
                    }else{
                        invoice.setUrl("");
                    }
                    if(!StringUtils.isEmpty(invoiceMap.get("KPRQ"))){
                        String invoiceDate = String.valueOf(invoiceMap.get("KPRQ"));
                        String invoiceDateFormat = qryCustInfoService.formatDateTime(invoiceDate);
                        invoice.setInvoiceDate(invoiceDateFormat);
                    }else{
                        invoice.setInvoiceDate("");
                    }
                    if(!StringUtils.isEmpty(invoiceMap.get("FP_DM"))){
                        String invoiceCode = String.valueOf(invoiceMap.get("FP_DM"));
                        invoice.setInvoiceCode(invoiceCode);
                    }else{
                        invoice.setInvoiceCode("");
                    }
                    if(!StringUtils.isEmpty(invoiceMap.get("FP_HM"))){
                        String invoiceNo = String.valueOf(invoiceMap.get("FP_HM"));
                        invoice.setInvoiceNo(invoiceNo);
                    }else{
                        invoice.setInvoiceNo("");
                    }
                    invoice.setInvoiceId(invoiceId);
                }
                return invoice;
            }else{
                String urlPara = "?invoiceId="+invoiceId+"&lanId="+lanId;
                invoice = getInvoiceByIdtoBill(urlPara,headers);
            }
        }else{
            String urlPara = "?invoiceId="+invoiceId+"&lanId="+lanId;
            invoice = getInvoiceByIdtoBill(urlPara,headers);
        }
        return invoice;
    }

    /**
     * 通过电子发票实例ID获取计费侧的发票信息——调计费侧接口
     * @param urlPara
     * @param headers
     * @return
     */
    public Invoice getInvoiceByIdtoBill(String urlPara,HttpHeaders headers){
        Invoice invoice = new Invoice();

        //调计费侧接口查询电子发票信息
        String paraId = "2";
        String url = invoiceInfoMapper.getInterfaceBill(paraId)+urlPara;
        //String url = "http://137.64.44.248:10010/esbWS/rest/sGrpEPrintQry"+urlPara;
        //设置请求体信息
        HttpEntity<String> requestEntity = new HttpEntity<String>("",headers);
        //发送请求
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Invoice> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Invoice.class);
        int resCode = response.getStatusCodeValue();
        if(StringUtil.isEqual(resCode,200)){
            invoice = response.getBody();
        }else{
            log.info("调发票查询接口失败码："+resCode);
            throw new RuntimeException("查询发票信息失败");
        }
        return invoice;

    }

    /**
     * 获取电子发票开具结果异步反馈结果
     * @param invoice
     * @return
     */
    public String getIssueInvoiceResult(Invoice invoice){
        String type = String.valueOf(invoice.getType());
        if(type.equals("1200")){

        }else{
            return "这儿调计费的接口";
        }
        return null;
    }

    //通过客户ID获取购买方的地址，开户行及纳税人标识
    public Map<String,String> doQryGmfInfo(String custId){

        String addsPhone = "";
        String GMF_YHZH = "";
        String GMF_NSRSBH = "";
        Map<String, String> custMap = invoiceInfoMapper.qryCustAddsPhone(custId);
        if (StringUtil.isNotEmptyOrNull(custMap)) {
            String certType = custMap.get("CERT_TYPE");
            Map<String, String> map = new HashMap<>();
            map.put("paraTypeId", "400014");
            map.put("paraId", certType);
            Integer count = invoiceInfoMapper.qryNoGernalCertType(map);
            String tmpStr = "";
            if (count == 0) { //证件类型不是营业执照、企业组织机构代码、税务登记号，取客户的证件地址
                if(StringUtil.isEqual("无",custMap.get("CERT_ADDRESS")) && StringUtil.isEqual("无",custMap.get("MOBILE_PHONE"))){
                    tmpStr = "";
                }else if(StringUtil.isEqual("无",custMap.get("CERT_ADDRESS")) && !StringUtil.isEqual("无",custMap.get("MOBILE_PHONE"))){
                    tmpStr = custMap.get("MOBILE_PHONE");
                }else if(!StringUtil.isEqual("无",custMap.get("CERT_ADDRESS")) && StringUtil.isEqual("无",custMap.get("MOBILE_PHONE"))){
                    tmpStr = custMap.get("CERT_ADDRESS");
                }else{
                    tmpStr = custMap.get("CERT_ADDRESS") + "," + custMap.get("MOBILE_PHONE");
                }
            } else { //取税务登记地址 \开户行和账号信息
                Map<String,String> attrMap = new HashMap<String,String>();
                attrMap.put("custId",custId);
                attrMap.put("attrId","999005");//税务登记地址
                String address0 = invoiceInfoMapper.getAttrValue(attrMap);
                if (StringUtil.isNotEmptyOrNull(address0)) {
                    String address = address0;
                    if(StringUtil.isEqual("无",address) && StringUtil.isEqual("无",custMap.get("MOBILE_PHONE"))){
                        tmpStr = "";
                    }else if(StringUtil.isEqual("无",address) && !StringUtil.isEqual("无",custMap.get("MOBILE_PHONE"))){
                        tmpStr = custMap.get("MOBILE_PHONE");
                    }else if(!StringUtil.isEqual("无",address) && StringUtil.isEqual("无",custMap.get("MOBILE_PHONE"))){
                        tmpStr = address;
                    }else{
                        tmpStr = address + "," + custMap.get("MOBILE_PHONE");
                    }
                }

                attrMap.put("attrId","999007");//税务开户银行名称
                String backName = invoiceInfoMapper.getAttrValue(attrMap);
                if (StringUtil.isNotEmptyOrNull(backName)) {
                        GMF_YHZH = backName + ",";
                }
                attrMap.put("attrId","999006");//税务开户银行账号
                String backNum = invoiceInfoMapper.getAttrValue(attrMap);
               if (StringUtil.isNotEmptyOrNull(backNum)) {
                    GMF_YHZH = GMF_YHZH + backNum;
                }
                attrMap.put("attrId","999002");//纳税人识别号
                String gmf_nsrbsh0 = invoiceInfoMapper.getAttrValue(attrMap);
               if (StringUtil.isNotEmptyOrNull(gmf_nsrbsh0)) {
                    GMF_NSRSBH = gmf_nsrbsh0;
                }
            }
            if (tmpStr.length() > 50) {
                addsPhone = tmpStr.substring(0, 50);
            } else {
                addsPhone = tmpStr;
            }
        }

        Map<String, String> resultMap = new HashMap<String,String>();
        resultMap.put("addsPhone", addsPhone);
        resultMap.put("GMF_YHZH",GMF_YHZH);
        resultMap.put("GMF_NSRSBH",GMF_NSRSBH);
        if(StringUtil.isNotEmptyOrNull(custMap.get("MOBILE_PHONE"))){
            resultMap.put("telPhone",StringUtil.obj2Str(custMap.get("MOBILE_PHONE")));
        }else{
            resultMap.put("telPhone",StringUtil.obj2Str(custMap.get("HOME_PHONE")));
        }

        return resultMap;
    }

    //用于测试insert语句
    public void test(){
//        Account account = new Account();
//        long accountId = 666;
//        long lanId = 1401;
//        long custId = 1843436539;
//        BigDecimal a= new BigDecimal("222");
//        account.setAccountId(accountId);
//        account.setStatusCd("1100");
//        account.setStatusDate(new Date());
//        account.setAccountType("0");
//        account.setAccountName("傻傻");
//        account.setAccountPasswd("wangdj");
//        account.setAccountLimit("A");
//        account.setRepresentPhone("17636652850");
//        account.setLanId(lanId);
//        account.setOpCode("移植");
//        account.setOpTime(new Date());
//        account.setLoginNo("admin");
//        account.setLoginAccept(a);
//        account.setPayCycle("100300");
//        account.setAccountingCycle("");
//        account.setIfDefault("2");
//        account.setContactPhone("");
//        account.setExtAccountId("");
//        account.setAccountNumber("");
//        account.setMobilePhone("");
//        account.setGroupId("");
//        account.setCommonRegionId(custId);
//        account.setAccountAreaGrade("");
//        account.setOpNotes("");
//        account.setExtProdInstId("");
//        account.setAccountNameAscii("");
//
//        account.setCustId(custId);
//        int b = accountMapper.insert(account);
//        b=20;
    }





}